 /**
 * @file tuya_zigbee_stack.h
 * @brief Common process - zigbee stack
 * @version 1.0.0
 * @date 2021-05-07
 *
 * @copyright Copyright 2021-2022 Tuya Inc. All Rights Reserved.
 *
 */

#ifndef __TUYA_ZIGBEE_STACK_H__
#define __TUYA_ZIGBEE_STACK_H__


#ifdef __cplusplus
    extern "C" {
#endif

#define TUYA_ZDO_ENDPOINT           0x00
#define TUYA_PRIMARY_ENDPOINT       0x01
#define TUYA_BROADCAST_ENDPOINT     0xFF

#define TUYA_GATEWAY_ADDRESS        0x0000

/**
 * the duration of scan period(the duration of channel reception), 
 * as an exponent.
 */
typedef enum {
    ZG_SCAN_DURATION_0 = 0, ///< 19.2 ms
    ZG_SCAN_DURATION_1,     ///< 38.4 ms
    ZG_SCAN_DURATION_2,     ///< 76.8 ms
    ZG_SCAN_DURATION_3,     ///< 153.6 ms
    ZG_SCAN_DURATION_4,     ///< 307.2 ms
    ZG_SCAN_DURATION_5,     ///< 614.4 ms
    ZG_SCAN_DURATION_6,     ///< 1.23 sec
    ZG_SCAN_DURATION_7,     ///< 2.46 sec
    ZG_SCAN_DURATION_8,     ///< 4.92 sec
    ZG_SCAN_DURATION_9,     ///< 9.83 sec
    ZG_SCAN_DURATION_10,    ///< 19.66 sec
    ZG_SCAN_DURATION_11,    ///< 39.32 sec
    ZG_SCAN_DURATION_12,    ///< 78.64 sec
    ZG_SCAN_DURATION_13,    ///< 157.28 sec
    ZG_SCAN_DURATION_14,    ///< 314.57 sec
} ZG_SCAN_DURATION_E;


/*
 *  ZCL data frame type
 */
typedef enum {
    ZG_ZCL_FRAME_TYPE_GLOBAL = 0,       ///< command acts across the entire profile
    ZG_ZCL_FRAME_TYPE_SPEC_TO_CLUSTER,  ///< command is specific to a cluster
} ZG_ZCL_FRAME_TYPE_E;

/*
 *  ZCL data direction
 */
typedef enum {
    ZG_ZCL_DATA_CLIENT_TO_SERVER = 0,   ///< server to client
    ZG_ZCL_DATA_SERVER_TO_CLIENT,       ///< client to server
} ZG_ZCL_DATA_DIRECTION_E;

/*
 *  zigbee node type
 */
typedef enum {
    ZG_ROUTER = 0,              ///< the device type is router
    ZG_END_DEVICE,              ///< the device type is end-device
    ZG_SLEEPY_END_DEVICE,       ///< the device type is end-device
    ZG_UNKNOWN_DEVICE = 0xFF,   ///< the device type unknown
} ZG_NODE_TYPE_E;

/*
 * zigbee network type
*/
typedef enum {
    ZG_DISTRIBUTE_NWK = 0,      ///< the zigbee network type is distribute network
    ZG_CENTRALIZED_NWK,         ///< the zigbee network type is centralized network
} ZG_NWK_TYPE_E;

/*
 * zigbee cluster mask
*/
typedef enum {
    ZG_CLUSTER_MASK_SERVER = 0x40,  ///< A server cluster
    ZG_CLUSTER_MASK_CLIENT = 0x80,  ///< A client cluster
} ZG_CLUSTER_MASK_E;

/*
 * Zigbee data delivery mode
*/
typedef enum {
    ZG_ADDR_UNKNOWN = 0x00,     ///< addr mode unknown when not joined network
    ZG_UNICAST_MODE,            ///< unicast addr mode for sending and receiving
    ZG_MULTICAST_MODE,          ///< multicast_ addr mode for sending and receiving
    ZG_BROADCAST_MODE,          ///< broadcast addr mode for sending and receiving
    ZG_BINDING_MODE,            ///< binding mode for sending
    ZG_MULTI_BINDING_MODE,      ///< multi binding mode for sending
} ZG_DELIVERY_MODE_E;


/*
 *  zigbee profile ID
 */
typedef enum {
    ZHA_PROFILE_ID = 0x0104,    ///< home automation
    ZGP_PROFILE_ID = 0xA1E0,    ///< green power
    ZHC_PROFILE_ID = 0x0108,    ///< personal,home and hospital care
    ZSE_PROFILE_ID = 0x0109,    ///< Smart energy
    ZCBA_PROFILE_ID = 0x0105,   ///< Commerical building automation
    ZIPM_PROFILE_ID = 0x0101,   ///< Industial plant monitoring
    ZTA_PROFILE_ID = 0x0107,    ///< Telecom application
    ZAMI_PROFILE_ID = 0x0109,   ///< Advanced metering initiative
    ZLL_PROFILE_ID = 0xC05E,    ///< ZLL profile
} ZG_PROFILE_ID_E;

/*
 * zigbee device ID
 */
typedef enum {
    ZG_DEVICE_ID_ON_OFF_SWITCH = 0x0000,        // on off switch
    ZG_DEVICE_ID_LEVEL_CONTROL_SWITCH = 0x0001,
    ZG_DEVICE_ID_ON_OFF_OUTPUT = 0x0002,
    ZG_DEVICE_ID_LEVEL_CONTROL_OUTPUT = 0x0003,
    ZG_DEVICE_ID_SCENE_SELECTOR = 0x0004,
    ZG_DEVICE_ID_CONFIG_TOOL = 0x0005,
    ZG_DEVICE_ID_REMOTE_CONTROL = 0x0006,
    ZG_DEVICE_ID_COMBINED_INTERFACE = 0x0007,
    ZG_DEVICE_ID_RANGE_EXTENDER = 0x0008,
    ZG_DEVICE_ID_MAINS_POWER_OUTLET = 0x0009,
    ZG_DEVICE_ID_DOOR_LOCK = 0x000a,
    ZG_DEVICE_ID_DOOR_LOCK_CONTROLLER = 0x000b,
    ZG_DEVICE_ID_SIMPLE_SENSOR = 0x000c,
    ZG_DEVICE_ID_CONSUMPTION_AWARENESS_DEVICE = 0x000d,

    ZG_DEVICE_ID_HOME_GATEWAY = 0x0050,
    ZG_DEVICE_ID_SMART_PLUG = 0x0051,
    ZG_DEVICE_ID_WHITE_GOODS = 0x0052,
    ZG_DEVICE_ID_METER_INTERFACE = 0x0053,

    ZG_DEVICE_ID_ON_OFF_LIGHT = 0x0100,             // on off light
    ZG_DEVICE_ID_DIMMABLE_LIGHT = 0x0101,
    ZG_DEVICE_ID_COLOR_DIMMABLE_LIGHT = 0x0102,
    ZG_DEVICE_ID_ON_OFF_LIGHT_SWITCH = 0x0103,
    ZG_DEVICE_ID_DIMMER_SWITCH = 0x0104,
    ZG_DEVICE_ID_COLOR_DIMMER_SWITCH = 0x0105,
    ZG_DEVICE_ID_LIGHT_SENSOR = 0x0106,
    ZG_DEVICE_ID_OCCUPANCY_SENSOR = 0x0107,
    ZG_DEVICE_ID_DUAL_TEMPERATURE_LIGHT = 0x010C,             // on off light
    ZG_DEVICE_ID_EXTENDED_COLOR_LIGHT = 0x010D,

    ZG_DEVICE_ID_SHADE = 0x0200,
    ZG_DEVICE_ID_SHADE_CONTROLLER = 0x0201,
    ZG_DEVICE_ID_WINDOW_COVERING_DEVICE = 0x0202,
    ZG_DEVICE_ID_WINDOW_COVERING_CONTROLLER = 0x0203,

    ZG_DEVICE_ID_HEATING_COOLING_UNIT = 0x0300,
    ZG_DEVICE_ID_THERMOSTAT = 0x0301,
    ZG_DEVICE_ID_TEMPERATURE_SENSOR = 0x0302,
    ZG_DEVICE_ID_PUMP = 0x0303,
    ZG_DEVICE_ID_PUMP_CONTROLLER = 0x0304,
    ZG_DEVICE_ID_PRESSURE_SENSOR = 0x0305,
    ZG_DEVICE_ID_FLOW_SENSOR = 0x0306,
    ZG_DEVICE_ID_MINI_SPLIT_AC = 0x0307,

    ZG_DEVICE_ID_IAS_CIE = 0x0400,
    ZG_DEVICE_ID_IAS_ANCILLARY_CONTROL = 0x0401,
    ZG_DEVICE_ID_IAS_ZONE = 0x0402,
    ZG_DEVICE_ID_IAS_WARNING = 0x0403,
} ZG_DEV_ID_E;

/*
 * zigbee cluster ID
 */
typedef enum {
    CLUSTER_BASIC_CLUSTER_ID = 0x0000,          // basic cluster id
    CLUSTER_POWER_CONFIG_CLUSTER_ID = 0x0001,
    CLUSTER_DEVICE_TEMP_CLUSTER_ID = 0x0002,
    CLUSTER_IDENTIFY_CLUSTER_ID = 0x0003,
    CLUSTER_GROUPS_CLUSTER_ID = 0x0004,
    CLUSTER_SCENES_CLUSTER_ID = 0x0005,
    CLUSTER_ON_OFF_CLUSTER_ID = 0x0006,         // on off cluster id
    CLUSTER_ON_OFF_SWITCH_CONFIG_CLUSTER_ID = 0x0007,
    CLUSTER_LEVEL_CONTROL_CLUSTER_ID = 0x0008,
    CLUSTER_ALARM_CLUSTER_ID = 0x0009,
    CLUSTER_TIME_CLUSTER_ID = 0x000A,
    CLUSTER_RSSI_LOCATION_CLUSTER_ID = 0x000B,
    CLUSTER_BINARY_INPUT_BASIC_CLUSTER_ID = 0x000F,
    CLUSTER_COMMISSIONING_CLUSTER_ID = 0x0015,
    CLUSTER_PARTITION_CLUSTER_ID = 0x0016,
    CLUSTER_OTA_BOOTLOAD_CLUSTER_ID = 0x0019,
    CLUSTER_POWER_PROFILE_CLUSTER_ID = 0x001A,
    CLUSTER_APPLIANCE_CONTROL_CLUSTER_ID = 0x001B,
    CLUSTER_POLL_CONTROL_CLUSTER_ID = 0x0020,
    CLUSTER_GREEN_POWER_CLUSTER_ID = 0x0021,
    CLUSTER_KEEPALIVE_CLUSTER_ID = 0x0025,
    CLUSTER_SHADE_CONFIG_CLUSTER_ID = 0x0100,
    CLUSTER_DOOR_LOCK_CLUSTER_ID = 0x0101,
    CLUSTER_WINDOW_COVERING_CLUSTER_ID = 0x0102,
    CLUSTER_BARRIER_CONTROL_CLUSTER_ID = 0x0103,
    CLUSTER_PUMP_CONFIG_CONTROL_CLUSTER_ID = 0x0200,
    CLUSTER_THERMOSTAT_CLUSTER_ID = 0x0201,
    CLUSTER_FAN_CONTROL_CLUSTER_ID = 0x0202,
    CLUSTER_DEHUMID_CONTROL_CLUSTER_ID = 0x0203,
    CLUSTER_THERMOSTAT_UI_CONFIG_CLUSTER_ID = 0x0204,
    CLUSTER_COLOR_CONTROL_CLUSTER_ID = 0x0300,
    CLUSTER_BALLAST_CONFIGURATION_CLUSTER_ID = 0x0301,
    CLUSTER_ILLUM_MEASUREMENT_CLUSTER_ID = 0x0400,
    CLUSTER_ILLUM_LEVEL_SENSING_CLUSTER_ID = 0x0401,
    CLUSTER_TEMP_MEASUREMENT_CLUSTER_ID = 0x0402,
    CLUSTER_PRESSURE_MEASUREMENT_CLUSTER_ID = 0x0403,
    CLUSTER_FLOW_MEASUREMENT_CLUSTER_ID = 0x0404,
    CLUSTER_RELATIVE_HUMIDITY_MEASUREMENT_CLUSTER_ID = 0x0405,
    CLUSTER_OCCUPANCY_SENSING_CLUSTER_ID = 0x0406,
    CLUSTER_CARBON_MONOXIDE_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x040C,
    CLUSTER_CARBON_DIOXIDE_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x040D,
    CLUSTER_ETHYLENE_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x040E,
    CLUSTER_ETHYLENE_OXIDE_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x040F,
    CLUSTER_HYDROGEN_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x0410,
    CLUSTER_HYDROGEN_SULPHIDE_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x0411,
    CLUSTER_NITRIC_OXIDE_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x0412,
    CLUSTER_NITROGEN_DIOXIDE_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x0413,
    CLUSTER_OXYGEN_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x0414,
    CLUSTER_OZONE_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x0415,
    CLUSTER_SULFUR_DIOXIDE_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x0416,
    CLUSTER_DISSOLVED_OXYGEN_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x0417,
    CLUSTER_BROMATE_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x0418,
    CLUSTER_CHLORAMINES_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x0419,
    CLUSTER_CHLORINE_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x041A,
    CLUSTER_FECAL_COLIFORM_AND_E_COLI_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x041B,
    CLUSTER_FLUORIDE_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x041C,
    CLUSTER_HALOACETIC_ACIDS_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x041D,
    CLUSTER_TOTAL_TRIHALOMETHANES_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x041E,
    CLUSTER_TOTAL_COLIFORM_BACTERIA_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x041F,
    CLUSTER_TURBIDITY_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x0420,
    CLUSTER_COPPER_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x0421,
    CLUSTER_LEAD_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x0422,
    CLUSTER_MANGANESE_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x0423,
    CLUSTER_SULFATE_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x0424,
    CLUSTER_BROMODICHLOROMETHANE_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x0425,
    CLUSTER_BROMOFORM_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x0426,
    CLUSTER_CHLORODIBROMOMETHANE_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x0427,
    CLUSTER_CHLOROFORM_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x0428,
    CLUSTER_SODIUM_CONCENTRATION_MEASUREMENT_CLUSTER_ID = 0x0429,
    CLUSTER_IAS_ZONE_CLUSTER_ID = 0x0500,
    CLUSTER_IAS_ACE_CLUSTER_ID = 0x0501,
    CLUSTER_IAS_WD_CLUSTER_ID = 0x0502,
    CLUSTER_GENERIC_TUNNEL_CLUSTER_ID = 0x0600,
    CLUSTER_BACNET_PROTOCOL_TUNNEL_CLUSTER_ID = 0x0601,
    CLUSTER_11073_PROTOCOL_TUNNEL_CLUSTER_ID = 0x0614,
    CLUSTER_ISO7816_PROTOCOL_TUNNEL_CLUSTER_ID = 0x0615,
    CLUSTER_PRICE_CLUSTER_ID = 0x0700,
    CLUSTER_DEMAND_RESPONSE_LOAD_CONTROL_CLUSTER_ID = 0x0701,
    CLUSTER_SIMPLE_METERING_CLUSTER_ID = 0x0702,
    CLUSTER_MESSAGING_CLUSTER_ID = 0x0703,
    CLUSTER_TUNNELING_CLUSTER_ID = 0x0704,
    CLUSTER_PREPAYMENT_CLUSTER_ID = 0x0705,
    CLUSTER_ENERGY_MANAGEMENT_CLUSTER_ID = 0x0706,
    CLUSTER_CALENDAR_CLUSTER_ID = 0x0707,
    CLUSTER_DEVICE_MANAGEMENT_CLUSTER_ID = 0x0708,
    CLUSTER_EVENTS_CLUSTER_ID = 0x0709,
    CLUSTER_MDU_PAIRING_CLUSTER_ID = 0x070A,
    CLUSTER_SUB_GHZ_CLUSTER_ID = 0x070B,
    CLUSTER_KEY_ESTABLISHMENT_CLUSTER_ID = 0x0800,
    CLUSTER_INFORMATION_CLUSTER_ID = 0x0900,
    CLUSTER_DATA_SHARING_CLUSTER_ID = 0x0901,
    CLUSTER_GAMING_CLUSTER_ID = 0x0902,
    CLUSTER_DATA_RATE_CONTROL_CLUSTER_ID = 0x0903,
    CLUSTER_VOICE_OVER_ZIGBEE_CLUSTER_ID = 0x0904,
    CLUSTER_CHATTING_CLUSTER_ID = 0x0905,
    CLUSTER_PAYMENT_CLUSTER_ID = 0x0A01,
    CLUSTER_BILLING_CLUSTER_ID = 0x0A02,
    CLUSTER_APPLIANCE_IDENTIFICATION_CLUSTER_ID = 0x0B00,
    CLUSTER_METER_IDENTIFICATION_CLUSTER_ID = 0x0B01,
    CLUSTER_APPLIANCE_EVENTS_AND_ALERT_CLUSTER_ID = 0x0B02,
    CLUSTER_APPLIANCE_STATISTICS_CLUSTER_ID = 0x0B03,
    CLUSTER_ELECTRICAL_MEASUREMENT_CLUSTER_ID = 0x0B04,
    CLUSTER_DIAGNOSTICS_CLUSTER_ID = 0x0B05,
    CLUSTER_ZLL_COMMISSIONING_CLUSTER_ID = 0x1000,
    CLUSTER_SAMPLE_MFG_SPECIFIC_CLUSTER_ID = 0xFC00,
    CLUSTER_OTA_CONFIGURATION_CLUSTER_ID = 0xFC01,
    CLUSTER_MFGLIB_CLUSTER_ID = 0xFC02,
    CLUSTER_SL_WWAH_CLUSTER_ID = 0xFC57,

    // tuya private cluster
    CLUSTER_TUYA_COMMON_CLUSTER_ID = 0xE000,
    CLUSTER_TUYA_ELECTRICIAN_CLUSTER_ID = 0xE001,
    CLUSTER_TUYA_SENSOR_CLUSTER_ID = 0xE002,
    CLUSTER_TUYA_LIGHT_CLUSTER_ID = 0xE003,
    
    CLUSTER_TUYA_MANAGER_CLUSTER_ID = 0xEE00,
    CLUSTER_PRIVATE_TUYA_CLUSTER_ID = 0xEF00,
    CLUSTER_TUYA_MF_TEST_CLUSTER_ID = 0xEEFF,
} ZG_CLUSTER_ID_E;

/*
 * Zigbee standard command status
*/
typedef enum {
  TUYA_ZCL_STA_SUCCESS                     = 0x00,
  TUYA_ZCL_STA_FAILURE                     = 0x01,
  TUYA_ZCL_STA_NOT_AUTHORIZED              = 0x7E,
  TUYA_ZCL_STA_MALFORMED_COMMAND           = 0x80,
  TUYA_ZCL_STA_UNSUP_COMMAND               = 0x81,
  TUYA_ZCL_STA_UNSUP_GENERAL_COMMAND       = 0x82,
  TUYA_ZCL_STA_UNSUP_MANUF_CLUSTER_COMMAND = 0x83,
  TUYA_ZCL_STA_UNSUP_MANUF_GENERAL_COMMAND = 0x84,
  TUYA_ZCL_STA_INVALID_FIELD               = 0x85,
  TUYA_ZCL_STA_UNSUPPORTED_ATTRIBUTE       = 0x86,
  TUYA_ZCL_STA_INVALID_VALUE               = 0x87,
  TUYA_ZCL_STA_READ_ONLY                   = 0x88,
  TUYA_ZCL_STA_INSUFFICIENT_SPACE          = 0x89,
  TUYA_ZCL_STA_DUPLICATE_EXISTS            = 0x8A,
  TUYA_ZCL_STA_NOT_FOUND                   = 0x8B,
  TUYA_ZCL_STA_UNREPORTABLE_ATTRIBUTE      = 0x8C,
  TUYA_ZCL_STA_INVALID_DATA_TYPE           = 0x8D,
  TUYA_ZCL_STA_INVALID_SELECTOR            = 0x8E,
  TUYA_ZCL_STA_WRITE_ONLY                  = 0x8F,
  TUYA_ZCL_STA_INCONSISTENT_STARTUP_STATE  = 0x90,
  TUYA_ZCL_STA_DEFINED_OUT_OF_BAND         = 0x91,
  TUYA_ZCL_STA_ACTION_DENIED               = 0x93,
  TUYA_ZCL_STA_TIMEOUT                     = 0x94,
  TUYA_ZCL_STA_ABORT                       = 0x95,
  TUYA_ZCL_STA_INVALID_IMAGE               = 0x96,
  TUYA_ZCL_STA_WAIT_FOR_DATA               = 0x97,
  TUYA_ZCL_STA_NO_IMAGE_AVAILABLE          = 0x98,
  TUYA_ZCL_STA_REQUIRE_MORE_IMAGE          = 0x99,
  TUYA_ZCL_STA_NOTIFICATION_PENDING        = 0x9A,
  TUYA_ZCL_STA_HARDWARE_FAILURE            = 0xC0,
  TUYA_ZCL_STA_SOFTWARE_FAILURE            = 0xC1,
  TUYA_ZCL_STA_UNSUPPORTED_CLUSTER         = 0xC3,
  TUYA_ZCL_STA_LIMIT_REACHED               = 0xC4,
} ZG_CMD_STATUS_E;


/*
 * zigbee attribute types
 */
#include "tuya_zigbee_attribute.h"

/*
 * zigbee command ID
 */
#include "tuya_zigbee_command.h"


#ifdef __cplusplus
}
#endif

#endif
